package com.cssw.fyzb.core.mao.live;

import com.cssw.fyzb.core.model.mos.LiveStatMo;
import com.cssw.fyzb.core.mongo.MongoClient;
import com.cssw.fyzb.core.util.DocUtil;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author YangYiQun
 */
@Component
public class LiveStatMaoImpl implements LiveStatMao {

    @Autowired
    private MongoClient mongoClient;

    @Override
    public LiveStatMo findByLiveId(Long liveId) {
        Document doc = mongoClient.getCollection("live_stat")
                .find(new Document("liveId", liveId))
                .first();
        return DocUtil.decoce(doc, LiveStatMo.class);
    }

    @Override
    public void create(Long liveId, Long userId, Long anchorId, String pull, String push, String accelerate) {
        LiveStatMo liveStatMo = new LiveStatMo();
        liveStatMo.setLiveId(liveId);
        liveStatMo.setUserId(userId);
        liveStatMo.setAnchorId(anchorId);
        liveStatMo.setStatus("1");
        liveStatMo.setLikes(0);
        liveStatMo.setViews(0);
        liveStatMo.setPk("0");
        liveStatMo.setLiveTime(new Date());
        liveStatMo.setPush(push);
        liveStatMo.setPull(pull);
        liveStatMo.setAccelerate(accelerate);
        mongoClient.getCollection("live_stat").insertOne(DocUtil.encode(liveStatMo));
    }

    @Override
    public void drop(Long liveId) {
        Document query = new Document();
        query.put("liveId", liveId);

        Document update = new Document();
        update.put("status", "2");
        update.put("completeTime", new Date());
        mongoClient.getCollection("live_stat").updateOne(query,
                new Document("$set", update));
    }

    @Override
    public void incLikes(Long liveId) {
        Document query = new Document()
                .append("liveId", liveId);
        Document set = new Document()
                .append("$inc", new Document().append("likes", 1));
        mongoClient.getCollection("live_stat").updateOne(query, set);
    }

    @Override
    public void incViews(Long liveId) {
        Document query = new Document()
                .append("liveId", liveId);
        Document set = new Document()
                .append("$inc", new Document().append("views", 1));
        mongoClient.getCollection("live_stat").updateOne(query, set);
    }

    @Override
    public void startPk(Long liveId, Long pkId, Long pkLiveId, Long pkUserId) {
        Document query = new Document()
                .append("liveId", liveId);
        Document update = new Document();
        update.put("pk", "1");
        update.put("pkId", pkId);
        update.put("pkLiveId", pkLiveId);
        update.put("pkUserId", pkUserId);
        mongoClient.getCollection("live_stat").updateOne(query, new Document("$set", update));
    }

    @Override
    public void stopPk(Long liveId) {
        Document query = new Document()
                .append("liveId", liveId);
        Document update = new Document();
        update.put("pk", 1);
        update.put("pkId", 1);
        update.put("pkLiveId", 1);
        update.put("pkUserId", 1);
        mongoClient.getCollection("live_stat").updateOne(query, new Document("$unset", update));
    }

    @Override
    public void sendRedPack(Long liveId, Long redPackId, Long redOpenTime) {
        Document query = new Document()
                .append("liveId", liveId);
        Document update = new Document();
        update.put("liveId", liveId);
        update.put("redPackId", redPackId);
        update.put("redOpenTime", redOpenTime);
        mongoClient.getCollection("live_stat").updateOne(query, new Document("$set", update));
    }
}
